package com.ruoyi.mall.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.vo.CustomerLevelVo;
import com.ruoyi.mall.domain.bo.CustomerLevelBo;
import com.ruoyi.mall.service.ICustomerLevelService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员等级
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/level")
public class CustomerLevelController extends BaseController {

    private final ICustomerLevelService iCustomerLevelService;

    /**
     * 查询会员等级列表
     */
    @SaCheckPermission("mall:level:list")
    @GetMapping("/list")
    public TableDataInfo<CustomerLevelVo> list(CustomerLevelBo bo, PageQuery pageQuery) {
        return iCustomerLevelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会员等级列表
     */
    @SaCheckPermission("mall:level:export")
    @Log(title = "会员等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CustomerLevelBo bo, HttpServletResponse response) {
        List<CustomerLevelVo> list = iCustomerLevelService.queryList(bo);
        ExcelUtil.exportExcel(list, "会员等级", CustomerLevelVo.class, response);
    }

    /**
     * 获取会员等级详细信息
     *
     * @param customerLevelId 主键
     */
    @SaCheckPermission("mall:level:query")
    @GetMapping("/{customerLevelId}")
    public R<CustomerLevelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long customerLevelId) {
        return R.ok(iCustomerLevelService.queryById(customerLevelId));
    }

    /**
     * 新增会员等级
     */
    @SaCheckPermission("mall:level:add")
    @Log(title = "会员等级", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CustomerLevelBo bo) {
        return toAjax(iCustomerLevelService.insertByBo(bo));
    }

    /**
     * 修改会员等级
     */
    @SaCheckPermission("mall:level:edit")
    @Log(title = "会员等级", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CustomerLevelBo bo) {
        return toAjax(iCustomerLevelService.updateByBo(bo));
    }

    /**
     * 删除会员等级
     *
     * @param customerLevelIds 主键串
     */
    @SaCheckPermission("mall:level:remove")
    @Log(title = "会员等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerLevelIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] customerLevelIds) {
        return toAjax(iCustomerLevelService.deleteWithValidByIds(Arrays.asList(customerLevelIds), true));
    }
}
